<template>
  <div class="rbody">
    <h2 class="tit" style="margin-bottom: 20px">账号注册</h2>
    <el-form ref="form" :rules="rules" :model="form" class="form" label-width="80px">
      <el-form-item label="账号" prop="account">
        <el-col :span="16">
          <el-input v-model="form.account" />
        </el-col>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-col :span="16">
          <el-input v-model="form.name" />
        </el-col>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-col :span="16">
          <el-input v-model="form.password" />
        </el-col>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-col :span="16">
          <el-input v-model="form.age" />
        </el-col>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.sex">
          <el-radio value="0">女</el-radio>
          <el-radio value="1">男</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.roleId" placeholder="请选择角色" style="width: 210px">
          <el-option label="超级管理员" value="0" />
          <el-option label="管理员" value="1" />
          <el-option label="用户" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-col :span="16">
          <el-input v-model="form.phone" />
        </el-col>
      </el-form-item>
      <div>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save"> 保存 </el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { ElMessageBox } from 'element-plus';
import { reactive } from 'vue';
export default {
  name: 'Register',
  data() {
    let checkAge = (rule, value, callback) => {
      if (value > 150) {
        callback(new Error('年龄输入过大'));
      } else {
        callback();
      }
    };
    let checkDuplicate = (rule, value, callback) => {
      if (this.form.id) {
        callback();
      }
      this.$axios
        .get(this.$httpUrl + '/user/findByAccount?account=' + this.form.account)
        .then((res) => res.data)
        .then((res) => {
          if (res.code != 200) {
            callback();
          } else {
            callback(new Error('账号已经存在'));
          }
        });
    };
    return {
      sexs: [
        {
          value: '1',
          label: '男',
        },
        {
          value: '0',
          label: '女',
        },
      ],
      form: {
        id: '',
        account: '',
        name: '',
        password: '',
        age: '',
        sex: '0',
        phone: '',
        roleId: '2',
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在1到8个字符', trigger: 'blur' },
          { validator: checkDuplicate, trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在1到8个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在1到15个字符', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { min: 1, max: 3, message: '长度在1到3个位', trigger: 'blur' },
          { pattern: /^([1-9][0-9]*){1,3}$/, message: '年龄必须为正整数字', trigger: 'blur' },
          { validator: checkAge, trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields();
    },
    login(a) {
      this.$axios
        .post(this.$httpUrl + '/user/login', a)
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            sessionStorage.setItem('User', JSON.stringify(res.data.user));
            this.$store.dispatch('updateMenu', res.data.menu);
            console.log(JSON.stringify(res.data.menu));
            this.$router.replace('/Index');
          } else {
            alert('出错了太快了');
            this.$router.replace('/');
            return false;
          }
        });
    },
    save() {
      console.log(this.form);
      this.$axios
        .post(this.$httpUrl + '/user/save', this.form)
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: '添加数据成功！',
              type: 'success',
            });
          } else {
            this.$message.error('添加数据失败！');
          }
        });
      this.login(this.form);
      this.resetForm();
    },
  },
};
</script>

<style scoped>
.rbody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #b3c0d1;
}
.tit {
  margin-top: 80px;
}
.form {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -250px;
  margin-left: -230px;
  width: 400px;
  height: 400px;
  padding: 30px 20px;
  background: #fff;
  border-radius: 5%;
}
</style>
